package com.ruoyi.web.controller.project;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProjectSchedule;
import com.ruoyi.system.service.IProjectScheduleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目进度Controller
 * 
 * @author zhaonz
 * @date 2023-06-02
 */
@Controller
@RequestMapping("/project/schedule")
public class ProjectScheduleController extends BaseController
{
    private String prefix = "project/schedule";

    @Autowired
    private IProjectScheduleService projectScheduleService;

    @RequiresPermissions("system:schedule:view")
    @GetMapping()
    public String schedule()
    {
        return prefix + "/schedule";
    }

    /**
     * 查询项目进度列表
     */
    @RequiresPermissions("system:schedule:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectSchedule projectSchedule)
    {
        startPage();
        List<ProjectSchedule> list = projectScheduleService.list(projectSchedule);
        return getDataTable(list);
    }

    /**
     * 导出项目进度列表
     */
    @RequiresPermissions("system:schedule:export")
    @Log(title = "项目进度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProjectSchedule projectSchedule)
    {
        List<ProjectSchedule> list = projectScheduleService.list(projectSchedule);
        ExcelUtil<ProjectSchedule> util = new ExcelUtil<ProjectSchedule>(ProjectSchedule.class);
        return util.exportExcel(list, "项目进度数据");
    }

    /**
     * 新增项目进度
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存项目进度
     */
    @RequiresPermissions("system:schedule:add")
    @Log(title = "项目进度", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProjectSchedule projectSchedule)
    {
        return toAjax(projectScheduleService.save(projectSchedule));
    }

    /**
     *
     * @param projectId
     * @return
     */
    @GetMapping("/detail/{projectId}")
    @ResponseBody
    public AjaxResult detail(@PathVariable("projectId") Long projectId)
    {
        long count = projectScheduleService.count(new QueryWrapper<ProjectSchedule>()
                .eq("project_id", projectId).eq("state", "complete"));
        return success(count);
    }

    /**
     * 修改项目进度
     */
    @RequiresPermissions("system:schedule:edit")
    @GetMapping("/edit/{scheduleId}")
    public String edit(@PathVariable("scheduleId") Long scheduleId, ModelMap mmap)
    {
        ProjectSchedule projectSchedule = projectScheduleService.getById(scheduleId);
        mmap.put("projectSchedule", projectSchedule);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目进度
     */
    @RequiresPermissions("system:schedule:edit")
    @Log(title = "项目进度", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectSchedule projectSchedule)
    {
        return toAjax(projectScheduleService.updateById(projectSchedule));
    }

    /**
     * 删除项目进度
     */
    @RequiresPermissions("system:schedule:remove")
    @Log(title = "项目进度", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(projectScheduleService.removeByIds(ids));
    }
}
